package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mastercard.assets.MasterCardException;
import com.mastercard.assets.jpa.TblAsset;
import com.mastercard.assets.jpa.TblCategoria;
import com.mastercard.assets.jpa.TblSubcateg;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.AssetAltaForm;
import com.mastercard.assets.web.struts.forms.AssetBorrarForm;
import com.mastercard.assets.web.struts.forms.AssetBuscarForm;
import com.mastercard.assets.web.struts.forms.LoginForm;
import com.mastercard.assets.web.struts.forms.PreimplementacionEventoForm;
import com.mastercard.assets.web.util.to.UserLogin;

public class PreimplementacionEventoAltaRegistrar
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		PreimplementacionEventoForm f = ( PreimplementacionEventoForm ) form ;
		int op = f.getOpcion( ) , pre = f.getPreimplementacion( )  ;
		System.out.println( "PreimplementacionEventoAltaRegistrar" ) ;
                System.out.println( "Opcion:::::> " + op ) ;		
                
		try
		{
                    TblAsset tblAsset = new TblAsset( ) ;
                    for( int asset : f.getAssets( ) )
                    {
                        tblAsset = MasterCardDAO.findAssetById( asset ) ;
                    }
                                        
//			MasterCardDAO.throwPreimplementation( user , null , f.getAssets( ) , f.getCveFlujo( ) ) ;
                    MasterCardDAO.throwImplementation( user , null , tblAsset , op , pre ) ;
                    
		}catch( MasterCardException e )
		{
                    MasterCardDAO.closeEntityManager( ) ;
			e.printStackTrace( ) ;
			
			user.setMessageError( e.getSingularMessage( ) ) ;
		}catch( Exception e )
		{
                    MasterCardDAO.closeEntityManager( ) ;
			e.printStackTrace( ) ;
			
			user.setMessageError( e.getMessage( ) ) ;
		}
		
		// en caso de que se cumpla, guarda el WorkArea
		user.setNextWorkArea( "/PreimplementacionEventoIn.do" ) ;
		
                MasterCardDAO.closeEntityManager( ) ;
	  	return mapping.findForward( "success" ) ;
	}
	
}
